<template>
  <!--会签管理-我的提交列表-材料结算编辑弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled" class="scroll-view" style="height: 600px">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title"
              style="margin-bottom: 10px;">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName"
              style="margin-bottom: 10px;">
              <a-select :options="levelOneOptions" placeholder="请选择项目" v-model="model.projectName"> </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierName"
              style="margin-bottom: 10px;">
              <a-select placeholder="请选择供应商" :options="levelTwoOptions" v-model="model.supplierName">
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractNumber"
              style="margin-bottom: 10px;">
              <a-input v-model="model.contractNumber" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="结算时间" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="period"
              style="margin-bottom: 10px;margin-left: 20px;">
              <div style="display: flex;align-items: center;">
                <a-select style="width: 314px;margin: 0 52px 0 2px;" placeholder="" :options="period"
                  v-model="model.period">
                </a-select>
                <j-date v-model="model.startTime" :showTime="true" date-format="YYYY-MM-DD "
                  placeholder="请选择开始时间"></j-date>
                <div style="width: 10px;margin: 0 10px;">~</div>
                <j-date v-model="model.endTime" :showTime="true" date-format="YYYY-MM-DD"
                  placeholder="请选择结束时间"></j-date>
              </div>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="材料项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialsProjectName"
              style="margin-bottom: 10px;">
              <a-input v-model="model.materialsProjectName" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="本期采购数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentPurchaseNumber"
              style="margin-bottom: 10px;">
              <a-input v-model="model.currentPurchaseNumber" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="本期金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentMoney"
              style="margin-bottom: 10px;">
              <a-input v-model="model.currentMoney" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="扣款或奖罚" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rewardAndPunish"
              style="margin-bottom: 10px;">
              <a-input v-model="model.rewardAndPunish" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="本期结算金额:" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="currentSettlementAmount" style="margin-bottom: 10px;">
              <a-input v-model="model.currentSettlementAmount" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="累计结算金额:" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="accumulatedSettlementAmount" style="margin-bottom: 10px;">
              <a-input style="" v-model="model.accumulatedSettlementAmount" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;margin-left: 20px;">
              <textarea :disabled="disabled" v-model="model.remark" cols="100" rows="3" style="width: 93%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="附件资料" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="attachment"
              style="margin:0 60px 10px 20px;">
              <j-upload v-model="model.attachment"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>

        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- <a-button @click="chexiao" type="primary" style="color: white; margin-left: 650px;">撤销</a-button> -->
    <!-- <a-button @click="submitForm1" type="primary" style="color: white; margin-left: 650px;">提交</a-button> -->
    <j-modal class="modal-privacy" :title="title" :visible="visible1" :disableScreenClick="true" okText="确定"
      cancelText="取消" @cancel="cancelPrivacy" @ok="confirmPrivacy">
      <textarea cols="70" rows="5" placeholder="请输入撤销原因"></textarea>

      <!-- <div style="font-size: 20px;width: 200px;height: 20px;margin: 0 auto;margin-top: 10px;">确定提交吗？</div> -->
    </j-modal>
  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'

  export default {
    name: 'WtBrandForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },
      modelDefault: {
        type: Object,
        default: () => ({}),
      }
    },
    data() {
      return {
        title: '撤销原因',
        model: {
          countersignId: '',
          projectName: '',
          supplierName: '',
          attachment: '',
          status: '1'
        },
        period: [{
            value: 1,
            label: "第1期"
          },
          {
            value: 2,
            label: "第2期"
          },

          {
            value: 3,
            label: "第3期"
          }, {
            value: 4,
            label: "第4期"
          },
          {
            value: 5,
            label: "第5期"
          },

          {
            value: 6,
            label: "第6期"
          }, {
            value: 7,
            label: "第7期"
          },
          {
            value: 8,
            label: "第8期"
          },

          {
            value: 9,
            label: "第9期"
          }, {
            value: 10,
            label: "第10期"
          },
          {
            value: 11,
            label: "第11期"
          },

          {
            value: 12,
            label: "第12期"
          }, {
            value: 13,
            label: "第13期"
          },
          {
            value: 14,
            label: "第14期"
          },

          {
            value: 15,
            label: "第15期"
          }, {
            value: 16,
            label: "第16期"
          },
          {
            value: 17,
            label: "第17期"
          },

          {
            value: 18,
            label: "第18期"
          }, {
            value: 19,
            label: "第19期"
          },
          {
            value: 20,
            label: "第20期"
          },

          {
            value: 21,
            label: "第21期"
          }, {
            value: 22,
            label: "第22期"
          },
          {
            value: 23,
            label: "第23期"
          },

          {
            value: 24,
            label: "第24期"
          }
        ],

        visible1: false,
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 2
          },
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 22
          },
        },
        confirmLoading: false,
        validatorRules: {},
        levelOneOptions: [],
        levelTwoOptions: [],
        // levelThreeOptions: [],
        treeData: [],
        // fileList: [],
        treeData1: [],
        treeData2: [],
        templateId: '',
        url: {
          add: '/wtBrand/add',
          edit: '/wtCountersign/countersignMaterialStatement/edit',
          anew: '/wtCountersign/countersignMaterialStatement/editSubmit',
          queryById: '/wtBrand/queryById',
        },
        pageType: 'edit', //edit-编辑 anew-重新提交
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      //this.modelDefault = JSON.parse(JSON.stringify(this.model))
      if (!!this.modelDefault) {
        this.model = JSON.parse(JSON.stringify(this.modelDefault));
      }
    },
    methods: {
      confirmPrivacy() {
        // console.log('同意了用户协议')
        // console.log(this.privacyDialogShow)l
        this.visible1 = false
        // console.log(this.privacyDialogShow)
      },
      cancelPrivacy() {
        // console.log('拒绝了用户协议')
        this.visible1 = false
      },
      loadTree(record) {
        var that = this
        getAction(`/wtCountersign/countersignMaterialStatement/queryByCountersignId?countersignId=${record.id}`).then(
          (res) => {
            if (res.success) {
              that.treeData1 = res.result
              // console.log('erferw',that.treeData1);
              this.model = that.treeData1
              // for (let a = 0; a < treeList1.length; a++) {
              //   // console.log('ewq2');
              //   let temp = treeList1[a]
              //   temp.isLeaf = temp.leaf
              //   that.treeData1.push(temp)
              // console.log('dwaq1',that.treeData1);
              getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
                if (res.success) {
                  that.treeData = []
                  let treeList = res.result
                  for (let a = 0; a < treeList.length; a++) {
                    let temp = treeList[a]
                    temp.isLeaf = temp.leaf
                    that.treeData.push(temp)
                  }

                  that.levelOneOptions = [...that.treeData].map((v) => {
                    return {
                      ...v,
                      value: v.id,
                      label: v.departName,
                    }
                  })
                  let info1 = that.levelOneOptions.find((item) => item.departName == this.model.projectName)
                  console.log('sew1', that.levelOneOptions, info1)

                  that.model.projectName = info1.id
                }
              })
              getAction(`/wtSupplier/list?type=0`).then((res) => {
                if (res.success) {
                  that.treeData2 = []
                  let treeList = res.result.records
                  for (let a = 0; a < treeList.length; a++) {
                    let temp = treeList[a]
                    temp.isLeaf = temp.leaf
                    that.treeData2.push(temp)
                  }

                  that.levelTwoOptions = [...that.treeData2].map((v) => {
                    // console.log('swq',v);
                    return {
                      ...v,
                      value: v.id,
                      label: v.name,
                    }
                  })
                  // console.log('dewq', that.levelTwoOptions)
                  var info = that.levelTwoOptions.filter((item) => {
                    return item.name == this.model.supplierName
                  })
                  // console.log('rew3',info);
                  that.model.supplierName = info[0].id
                  // that.model.supplierName = info[0].value
                }
              })
            }
          }
        )
      },
      add() {
        this.edit(this.modelDefault)
      },
      edit(record, type) {
        // this.model = Object.assign({}, record)
        console.log(123, record, type)
        if (!!type) {
          this.pageType = type;
        }
        this.visible = true
        this.loadTree(record)
        this.templateId = record.id
      },
      chexiao() {
        this.visible1 = true
      },
      submitForm(record) {
        const that = this
        if (!this.model.attachment) {
          that.$message.warning('附件资料不能为空,请上传')
        } else {
          // 触发表单验证
          this.$refs.form.validate((valid) => {
            if (valid) {
              that.confirmLoading = true
              let httpurl = ''
              if (that.pageType === 'edit') {
                httpurl = that.url.edit
              } else if (that.pageType === 'anew') {
                httpurl = that.url.anew
              }
              that.model.countersignId = that.templateId
              that.model.projectId = that.model.projectName
              that.model.supplierId = that.model.supplierName
              that.model.list = that.model.list
          
              that.model.status = record;
              console.log('提交表单', that.model)
              httpAction(httpurl, that.model, 'POST')
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          })
        }
        
      },
    },
  }
</script>
<style scoped>
  .scroll-view {
    overflow: auto;
    overflow-y: scroll;
  }
</style>